package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson2.JSON;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.logging.Handler;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private UserDao userDao;

    @Override
    public ServiceVO damageListGoods(DamageList damageList, String damageListGoodsStr, Integer userId) {
        damageList.setUserId(userId);
        List originList = JSON.parseObject(damageListGoodsStr, List.class);
        //保存损坏表
        damageListGoodsDao.saveDamageList(damageList);
        originList.stream().forEach(list -> {
            DamageListGoods damageListGoods = JSON.parseObject(String.valueOf(list), DamageListGoods.class);
            damageListGoods.setDamageListId(damageList.getDamageListId());
            //保存损坏表对应的商品表
            damageListGoodsDao.saveDamageListGoods(damageListGoods);
        });
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 新增溢单表
     *
     * @param overflowList
     * @param overflowListGoodsStr
     * @param userId
     * @return
     */
    @Override
    public ServiceVO overflowListGoodsSave(OverflowList overflowList, String overflowListGoodsStr, Integer userId) {

        overflowList.setUserId(userId);
        List originList = JSON.parseObject(overflowListGoodsStr, List.class);
        //保存损坏表
        damageListGoodsDao.saveOverflowList(overflowList);
        originList.stream().forEach(list -> {
            OverflowListGoods overflowListGoods = JSON.parseObject(String.valueOf(list), OverflowListGoods.class);
            overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
            //保存损坏表对应的商品表
            damageListGoodsDao.saveOverFlowGoods(overflowListGoods);
        });
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 根据时间 查询报损单查询
     *
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> damageListGoodsList(String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<DamageList> damageList111 = new ArrayList<>();
        List<DamageList> damageList = damageListGoodsDao.getDamageList();
        if (!CollectionUtils.isEmpty(damageList)) {
            damageList.stream().forEach(damageList1 -> {
                String damageDate = damageList1.getDamageDate();
                Boolean flag = this.compareDate(damageDate, sTime, eTime);
                if (flag) {
                    Integer userId = damageList1.getUserId();
                    User user = userDao.getUserById(userId);
                    if (user != null) {
                        String userName = user.getTrueName();
                        damageList1.setTrueName(userName);
                    }
                    damageList111.add(damageList1);
                }
            });
            map.put("rows", damageList111);
        }
        return map;
    }

    /**
     * 查询报损单商品信息
     *
     * @param damageListId
     * @return
     */
    @Override
    public Map<String, Object> damageListGoodsGoodsList(Integer damageListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<DamageListGoods> damageListGoodsList = damageListGoodsDao.damageListGoodsGoodsList(damageListId);
        if (!CollectionUtils.isEmpty(damageListGoodsList)) {
            map.put("rows", damageListGoodsList);
        }
        return map;
    }

    /**
     *报溢单订单信息
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> overflowListGoods(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<OverflowList> overflowLists = new ArrayList<OverflowList>();
        List<OverflowList> damageListGoodsList=  damageListGoodsDao.overflowListGoods();
       if (!CollectionUtils.isEmpty(damageListGoodsList)) {
           damageListGoodsList.stream().forEach(damageListGoods -> {
               String overflowDate = damageListGoods.getOverflowDate();
               Boolean flag = this.compareDate(overflowDate, sTime, eTime);
               User userById = userDao.getUserById(damageListGoods.getUserId());
               damageListGoods.setTrueName(userById.getTrueName());
               if (flag){
                   overflowLists.add(damageListGoods);
               }
           });
           map.put("rows", overflowLists);
       }
        return map;
    }




    /**
     * 查询报溢单商品信息
     * @param overflowListId
     * @return
     */
    @Override
    public Map<String, Object> overflowListGoodsgoodsList(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowListGoods> damageListGoodsList = damageListGoodsDao.overflowListGoodsgoodsList(overflowListId);
        if (!CollectionUtils.isEmpty(damageListGoodsList)) {
            map.put("rows", damageListGoodsList);
        }
        return map;

    }

    /**
     * 判断当前时间是否在输入时间之内
     *
     * @param damageDate
     * @param sTime
     * @param eTime
     * @return
     */
    private Boolean compareDate(String damageDate, String sTime, String eTime) {
        //1 当前时间在 前面时间在 后面时间的 前面
        //-1   当前时间在 前面时间在 后面时间的 后面
        int s = DateUtils.compareDate(damageDate, sTime);
        int e = DateUtils.compareDate(damageDate, eTime);
        if (e == 0) {
            return true;
        }
        ;
        if (s == -1 && e == 1) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String sTime = "1999-12-11";
        String sTime1 = "1999-12-13";
        int i = DateUtils.compareDate(sTime, sTime1);

        System.out.println(i + "====================");
    }
}
